// $codepro.audit.disable assignmentToNonFinalStatic, initializeStaticFields, staticFieldNamingConvention
/**LoginPresenter**/
// $codepro.audit.disable variableUsage, com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.alwaysOverridetoString.alwaysOverrideToString, expressionValue
package edu.gatech.presenters;

import javax.swing.JOptionPane;

import edu.gatech.applications.Login;
import edu.gatech.gui.AdminPanel;
import edu.gatech.gui.DoctorPanel;
import edu.gatech.gui.NursePanel;
import edu.gatech.gui.PatientPanel;
import edu.gatech.userclasses.Doctor;
import edu.gatech.userclasses.Nurse;
import edu.gatech.userclasses.Patient;
import edu.gatech.userclasses.SysAdmin;
import edu.gatech.userclasses.User;
import edu.gatech.utilities.Persistence;

/**
 * Presenter for the login GUI
 * @author Tyler Natoli
 * @version 1.0
 */
public class LoginPresenter {
	/**Username to attempt login**/
	private final String username;
	
	/**Password for attempted login**/
	private final String password;
	
	/**Login object to log into system**/
	private Login login;
	
	/**Counter to suspend a user after too many login attempts**/
	private static int counter;
	
	/**String keeping track of previous login attempt**/
	private static String previousUser;
	
	/**Maximum attempts before user is locked out**/
	private static final int MAX_LOGIN_ATTEMPTS = 2;
	
	/**
	 * Constructor that gets values from GUI
	 * @param username
	 * @param password
	 */
	public LoginPresenter(String username, String password){
		this.username = username;
		this.password = password;
	}
	
	/**
	 * Logs into the system using the information from the GUI
	 */
	public void login(){
		login = new Login(username, password);
		final User user = login.validate();
		if(counter == MAX_LOGIN_ATTEMPTS){
			counter = 0;
			User currentUser = null;
			for (User u : Persistence.getUserList()){
				if (username.equals(u.getName())){
					currentUser = u;
				}
			}
			if(currentUser == null){
				JOptionPane.showMessageDialog(null, "We do not have" +
						"this username in our system. Please register.");
			}
			else{
				currentUser.setSuspended(true);
				Persistence.save();
				JOptionPane.showMessageDialog(null, "You have attempted to login " +
						"too many times. " +
						"Your account has been suspended. Please what for a system " +
						"adminastrator to grant" +
						" you access.");
			}
		}
		else if(user != null){
			if(!user.isSuspended()){
				counter = 0;
				if(user.getUserType().equals("Doctor")){
					final DoctorPanel doc = new DoctorPanel((Doctor) user);
				}
				else if(user.getUserType().equals("Patient")){
					final PatientPanel patient = new PatientPanel((Patient) user);
				}
				else if(user.getUserType().equals("Nurse")){
					final NursePanel nurse = new NursePanel((Nurse) user);
				}
				else{
					final AdminPanel admin = new AdminPanel((SysAdmin) user);
				}
			}
			else{
				JOptionPane.showMessageDialog(null, "Sorry your account has " +
						"been suspended. Please wait for a system admin to " +
						"grant you access.");
			}
		}
		else{
			if(login.getUsername().equals(previousUser)){
				counter++;
			}
			else{
				counter = 0;
			}
			previousUser = login.getUsername();
			JOptionPane.showMessageDialog(null, "Invalid username or password");
		}
	}
}
